package com.mdc.mansion.api.web.modules.restaurant;

import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.core.modules.restaurant.RestaurantStaffService;
import com.mdc.mansion.core.modules.restaurant.dto.RestaurantStaffDTO;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantStaff;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/staff")
public class RestaurantStaffController extends BaseController {

	@Autowired
	private RestaurantStaffService restaurantStaffService;

	@ModelAttribute("restaurantStaffDTO")
	public RestaurantStaffDTO getRestaurantStaffDTO(@RequestParam(required = false) String data) {
		if(StringUtils.isBlank(data)){return null;}
		RestaurantStaffDTO restaurantStaffDTO= JSONObject.parseObject(data,RestaurantStaffDTO.class);
		return restaurantStaffDTO;
	}
	@RequestMapping("list")
	public Result list(){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		List<RestaurantStaffDTO> staffs=restaurantStaffService.currentUserRestaurantStaffs();
		result.setProperty("staffs",staffs);
		return result;
	}

	@RequestMapping("save")
	public Result save(@ModelAttribute("restaurantStaffDTO")RestaurantStaffDTO restaurantStaffDTO){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantStaffService.saveCurrentUserRestaurantStaff(restaurantStaffDTO);
		return result;
	}


	@RequestMapping("delete")
	public Result delete(@ModelAttribute("restaurantStaffDTO")RestaurantStaffDTO restaurantStaffDTO){ //需要考虑删除其他餐厅员工异常情况
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		RestaurantStaff restaurantStaff=new RestaurantStaff();
		restaurantStaff.setId(restaurantStaffDTO.getId());
		restaurantStaffService.delete(restaurantStaff);
		return result;
	}

	@RequestMapping("callStaff")
	public Result callStaff(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantStaffService.callStaff(data);
		return result;
	}
}
